package com.starwish.starwishblog.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.starwish.starwishblog.entity.Tag;
import com.starwish.starwishblog.mapper.TagMapper;


import com.starwish.starwishblog.vo.TagVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TagService {

    @Autowired
    TagMapper tagMapper;

    public Tag findById(Integer id){
        return tagMapper.selectByPrimaryKey(id);
    }

    public void removeBykey(Integer id) {
       tagMapper.deleteById(id);
    }

    public void addTag(Tag tag) {
        tagMapper.insert(tag);
    }

    public PageInfo<Tag> showTagByPage(Integer now, Integer size, String name) {
        PageHelper.startPage(now, size);
        List<Tag> tags  = tagMapper.selectTagByCons(name);
        return new PageInfo<>(tags);

    }

    public void modifytag(Tag tag) {
        tagMapper.updateByPrimaryKey(tag);
    }

    @Transactional
    public int addTagFront(Integer articleId, Integer userId, String tagName) {
        Tag tag = new Tag();
        tag.setName(tagName);
        tag.setAuthorId(userId);
        int i = tagMapper.insertTagFront(tag);

        return tagMapper.insertIntoArticleTag(articleId,tag.getId());

    }

    @Transactional
    public int removeById(Integer id) {
        Integer integer = tagMapper.deleteByTid(id);
        return tagMapper.deleteArticleTagByTid(id);


    }

    public List<Tag> getHotTags() {
        return tagMapper.selectHotTags();
    }
}
